package com.doge.service.admin.aftersale;


import com.doge.bean.base.BaseData;
import com.doge.bean.base.BaseParam;
import com.doge.bean.bo.aftersale.AftersaleBatchBO;
import com.doge.bean.pojo.aftersale.AfterSale;
import com.doge.bean.pojo.aftersale.AfterSaleExample;
import com.doge.bean.pojo.order.Order;
import com.doge.mapper.AfterSaleMapper;
import com.doge.mapper.OrderMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

/**
 * @ClassName: AftersaleServiceImpl
 * @Description:
 * @author: PuJing
 * @create: 2021-11-29-22:38
 **/
@Service
public class AftersaleServiceImpl implements AftersaleService {


    @Autowired
    AfterSaleMapper afterSaleMapper;
    @Autowired
    OrderMapper orderMapper;

    @Override
    public BaseData list(BaseParam param, Integer aftersaleSn, Integer orderId, Integer status) {
        // 在查询之前使用分页！！！
        PageHelper.startPage(param.getPage(), param.getLimit());

        //List<AfterSale> afterSales = aftersaleService.list(aftersaleSn , orderId , param.getSort(), param.getOrder());

        AfterSaleExample example = new AfterSaleExample();

        // 👉 List<Criterion> 条件列表
        //Criteria提供的and方法，就是向条件列表里塞入数据

        AfterSaleExample.Criteria criteria = example.createCriteria();

        if (aftersaleSn != null) {
            criteria.andAftersaleSnEqualTo(aftersaleSn);
        }
        if (orderId != null) {
            criteria.andOrderIdEqualTo(orderId);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }

        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<AfterSale> afterSales = afterSaleMapper.selectByExample(example);

        //long total = userMapper.countByExample(example);

        // 不需要去查询拿到total，通过分页信息拿到分页数据
        // 用PageInfo的有参构造方法，放入的是 查询结果！！，然后就可以获得这一次查询的分页信息
        PageInfo info = new PageInfo(afterSales);
        // 获得total， 查询获得的总数据量total >= users.size 分页后的一页的数据量(。
        //                   当total<=limit时 ，total=users.size
        long total = info.getTotal();

        return BaseData.list(afterSales, total);

    }


    @Override
    public int recept(AfterSale afterSale, Short status) {

        Integer id = afterSale.getId();
        int row = afterSaleMapper.updateById(id, status);

        Order order = orderMapper.selectByPrimaryKey(afterSale.getOrderId());

        order.setUpdateTime(new Date());
        // 还要更改order的售后状态aftersaleStatus
        order.setAftersaleStatus((short)2);

        orderMapper.updateByPrimaryKeySelective(order);


        if (row == 1) {
            return 200;
        } else {
            return 621;
        }
    }

    @Override
    public int reject(AfterSale afterSale, Short status) {

        Integer id = afterSale.getId();
        int row = afterSaleMapper.updateById(id, status);

        Order order = orderMapper.selectByPrimaryKey(afterSale.getOrderId());

        order.setUpdateTime(new Date());
        // 还要更改order的售后状态aftersaleStatus
        order.setAftersaleStatus((short)4);

        orderMapper.updateByPrimaryKeySelective(order);

        if (row == 1) {
            return 200;
        } else {
            return 621;
        }
    }

    @Override
    public int batch_reject(AftersaleBatchBO aftersaleBatchBO) {

        Integer[] ids = aftersaleBatchBO.getIds();

        Short status = 4;

        int row = 0;

        for (Integer id : ids) {

            row = afterSaleMapper.updateById(id, status);


            AfterSale afterSale = afterSaleMapper.selectByPrimaryKey(id);

            Order order = orderMapper.selectByPrimaryKey(afterSale.getOrderId());

            order.setUpdateTime(new Date());
            // 还要更改order的售后状态aftersaleStatus
            order.setAftersaleStatus((short)2);

            orderMapper.updateByPrimaryKeySelective(order);

            if (row == 0) {

                return 621;
            }
        }

        return 200;
    }

    @Override
    public int batch_recept(AftersaleBatchBO aftersaleBatchBO) {
        Integer[] ids = aftersaleBatchBO.getIds();

        Short status = 2;

        int row = 0;

        for (Integer id : ids) {

            row = afterSaleMapper.updateById(id, status);

            AfterSale afterSale = afterSaleMapper.selectByPrimaryKey(id);

            Order order = orderMapper.selectByPrimaryKey(afterSale.getOrderId());

            order.setUpdateTime(new Date());
            // 还要更改order的售后状态aftersaleStatus
            order.setAftersaleStatus((short)4);

            orderMapper.updateByPrimaryKeySelective(order);


            if (row == 0) {

                return 621;
            }
        }

        return 200;

    }

    @Override
    public int refund(AfterSale afterSale, Short status) {

        Integer id = afterSale.getId();

        int row = afterSaleMapper.updateById(id, status);

        Order order = orderMapper.selectByPrimaryKey(afterSale.getOrderId());

        order.setUpdateTime(new Date());
        // 还要更改order的售后状态aftersaleStatus
        order.setAftersaleStatus((short)3);
        // 还要更改order的refund退款时间
        order.setRefundTime(new Date());

        orderMapper.updateByPrimaryKeySelective(order);


        if (row == 1) {
            return 200;
        } else {
            return 621;
        }


    }
}
